<?php

require_once BASE_URL . '/config/config.php';
require_once BASE_URL . '/core/basefunction.php';


/**
 * 静态资源
 */
$url = $_SERVER['SERVER_NAME'];
define("__PUBLIC__", "/public/");
define("__HOST__",$_SERVER['HTTP_HOST']);


//取默认配置
$module = isset($config['default_module']) ? $config['default_module'] : 'Home';
$controller = isset($config['default_controller']) ? $config['default_controller'] : 'Index';
$action = isset($config['default_action']) ? $config['default_action'] : 'Index';


$uri = explode('/', trim($_SERVER['REQUEST_URI'], '/'));

//处理分模块
$moduleList = $config['module_list'];

//自定义url路由规则
if (isset($uri[0]) && in_array(ucwords($uri[0]), $moduleList)) {
    $module = $uri[0];
    unset($uri[0]);
    $uri = array_values($uri);

}

$p = BASE_URL . '/' . APP_PATH . '/' . $module . '/Controller/' . $action . 'Controller.php';
$c = isset($uri[0]) && $uri[0]  ? $uri[0] : $controller;
$a = isset($uri[1]) ? $uri[1] : $action;



//get取参
$uri = array_slice($uri,2);

foreach($uri as $k => $v){

    if($k % 2 == 1){

        $GET[$uri[$k-1]] = $v;

    }else{

        $_GET[$v] = isset($uri[$k + 1]) ? $uri[$k+1]:'';

    }
}

session_start();
//自动加载
$c = ucwords($c) . 'Controller';
define('DEFAULT_MODULE', $module);
function autoload($class)
{
    $p = '/Controller/';

    if (preg_match($p, $class)) {
        require BASE_URL . '/' . APP_PATH . '/' . DEFAULT_MODULE . '/Controller/' . $class . '.php';
    } else {
        require BASE_URL . '/' . APP_PATH . '/' . DEFAULT_MODULE . '/Model/' . $class . '.php';
    }

}

spl_autoload_register('autoload');

require_once $p;
(new $c())->$a();


